<?php

// Load the statement class
require_once NBWI_ROOT . 'nbwiStatement.php';

class Extension
{
	protected static
		$dbConn,
		$dbPrefix;
	
	protected
		$fields = array('*'),
		$where	= array(),
		$limit	= array(),
		$order	= array();
	
	public function __construct($host, $user, $pass, $database, $prefix)
	{
		if (!is_resource(self::$dbConn))
		{
			// Make the databse connection
			if ($this->dbConnect($host, $user, $pass))
			{
				mysql_set_charset('utf8', self::$dbConn);
				
				$this->dbSelect($database);
			}
			
			self::$dbPrefix = $prefix;
		}
	}
	
	public function __call($method, $args = array())
	{
		// Small hack for functions which pass arrays
		if (isset($args[0]) && is_array($args[0]))
		{
			$args = $args[0];
		}
		
		$this->$method = $args;
		
		return $this;
	}
	
	private function buildSql()
	{
		$sql = 'SELECT ';
		$sql .= implode(', ', $this->makeSafe($this->fields));
		$sql .= ' FROM ' . self::$dbPrefix . $this->table;
		
		if (!empty($this->where))
		{
			$sql .= ' WHERE ' . implode(' AND ', $this->where);
		}
		
		if (!empty($this->order))
		{
			$sql .= ' ORDER BY ' . implode(', ', $this->makeSafe($this->order));
		}
		
		if (!empty($this->limit))
		{
			$sql .= ' LIMIT ' . $this->limit[0];
			
			if ($this->limit[1] != '')
			{
				$sql .= ' OFFSET ' . $this->limit[1];
			}
		}
		
		return $sql;
	}
	
	private function dbConnect($host, $user, $pass)
	{
		$conn = @mysql_connect($host, $user, $pass);
		
		if (!$conn)
		{
			throw new nbwiException(
				'Could not connect to the database.<br /><br />Mysql Error: ' . mysql_error(),
				nbwiException::DATABASE_ERROR
			);
		}
		
		self::$dbConn = $conn;
		
		return true;
	}
	
	private function dbError()
	{
		return mysql_error(self::$dbConn);
	}
	
	private function dbSelect($database)
	{
		if (!mysql_select_db($database, self::$dbConn))
		{
			throw new nbwiException(
				'Could not select the database.<br /><br />Mysql Error: ' . $this->dbError(),
				nbwiException::DATABASE_ERROR
			);
		}
		
		return true;
	}
	
	public function execute($reset = true)
	{
		if (func_num_args() > 1)
		{
			// Process fields
			$args = func_get_args();
			array_shift($args);
			
			$this->fields($args);
		}
		
		$sql = $this->buildSql();
		
		if (!($query = mysql_query($sql, self::$dbConn)))
		{
			throw new nbwiException($this->dbError(), nbwiException::DATABASE_ERROR);
		}
		
		if ($reset == true)
		{
			$this->reset();
		}
		
		return new nbwiStatement(self::$dbConn, $query);
	}
	
	public function load()
	{
		$this->fields($args = func_get_args());
		
		return $this;
	}
	
	public function makeSafe($data)
	{
		if (func_num_args() > 1)
		{
			$return = array();
			
			for ($i = 0; $i < func_num_args(); ++$i)
			{
				$return[] = $this->makeSafe($arg = func_get_arg($i));
			}
			
			return $return;
		}
		else
		{
			if (is_array($data))
			{
				$return = array();
				
				foreach ($data as $key => $value)
				{
					$return[$key] = $this->makeSafe($value);
				}
				
				return $return;
			}
		}
		
		return mysql_real_escape_string($data, self::$dbConn);
	}
	
	public function reset()
	{
		$this->fields	= array('*');
		$this->limit	= array();
		$this->order	= array();
	}
}

?>